package com.rabbitmq.ttl;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.rabbitmq.util.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import static com.rabbitmq.common.Constant.NUM20000;

/**
 * 队列过期
 */
@Slf4j
public class QueueTTL {
    /**
     * 交换器
     */
    private static final String EXCHANGE_NAME = "exchange_demo";
    /**
     * 路由键
     */
    private static final String ROUTING_KEY = "routingkey_demo";
    /**
     * 队列
     */
    private static final String QUEUE_NAME = "QueueTTL_demo";

    public static void main( String[] args ) {
        try {
            Connection connection = ConnectionUtils.getConnection();
            Channel channel = connection.createChannel();
            // 创建一个 type="direct" 、持久化的、非自动删除的交换器
            channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
            // 创建一个持久化、非排他的、非自动删除的队列
            Map<String, Object> arg = new HashMap<String, Object>();
            // 设置队列过期时间，单位：毫秒
            arg.put("x-expires", NUM20000);
            channel.queueDeclare(QUEUE_NAME, true, false, false, arg);
            // 将交换器与队列通过路由键绑定
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
            // 发送一条持久化的消息: hello world !
            String message = "Hello World !";
            // 第三个参数为mandatory
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, false,
                    MessageProperties.PERSISTENT_TEXT_PLAIN,
                    message.getBytes());

            ConnectionUtils.close(channel, connection);
        } catch (IOException e) {
            log.info("catch IOException");
        } catch (TimeoutException e) {
            log.info("catch TimeoutException");
        }
    }
}
